package com.focus.xyz.other.cases;

import com.focus.xyz.core.common.listeners.ErrorDataListener;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.core.utils.ExcelUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.other.pages.tpytb.baby.HomePage;
import com.focus.xyz.other.pages.tpytb.baby.MainPage;
import com.focus.xyz.other.pages.tpytb.baby.TBPage;
import com.focus.xyz.union.action.KeyWordAction;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.*;

/**
 * Created by chenmeng on 2016/5/16.
 */
@Listeners({ErrorDataListener.class})
public class BabyCardTest {
    private WebDriver driver;
    private KeyWordAction action;
    private MainPage mainPage;
    private HomePage homePage;
    private TBPage tbPage;


    @Parameters({"nodeUrl","browser"})
    @BeforeClass(alwaysRun = true)
    public void setUp(String nodeUrl,String browser) throws Exception{
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        action = new KeyWordAction(driver);
        mainPage = new MainPage(driver,action);
        mainPage.loginSystem("NAJUVG","Abcd1111");
        homePage = new HomePage(driver,action);
        homePage.switchTop();
        homePage.clickfntb();
        homePage.switchLeft();
        homePage.clickDir();
        homePage.clickBx1();
        homePage.switchRight();
    }

    @Parameters({"browser"})
    @AfterClass(alwaysRun = true)
    public void tearDown(String browser){
        try{
            if(driver!=null){
                driver.quit();
            }
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }
    }


    @DataProvider(name = "data")
    public Object[][] getData() throws Exception{
        String[][] data = null;
        try {
            ExcelUtils eu = new ExcelUtils(ConfigPropInfo.TEST_DATA_DIR + ConfigPropInfo.BABY_CARD_DATA_FILE);
            eu.setSelectedSheetName("data");
            eu.setOnlyReadOneSheet(true);
            eu.setStartReadPos(1);
            data = eu.readExcelArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }


    @Test(dataProvider = "data",enabled = false)
    public void toubao(String tbr,String tbrcardNo,String insuredName,String insuredCardNo,String count,String email,String schooName) throws Exception{
        if(StringUtils.isNotBlank(count)&&"1".equals(count)){
            tbPage = new TBPage(driver,action);
            tbPage.clickNotSelf();
            tbPage.fillBaseInfo(tbr,tbrcardNo,email);
            tbPage.fillInsuredInfo(insuredName,insuredCardNo);
            tbPage.inputSchoolName(schooName);
            tbPage.submit();
            action.pause(2000l);
            action.switchToWindowByUrl("http://ebiz.cpic.com.cn/ywx/insurencePrecept.do?method=electronicPrintPage",false);
            tbPage.confirm();
            action.pause(3000l);
            tbPage.print();
            action.pause(2000l);
            action.acceptAlert();
            try{
                action.pause(1500l);
                action.switchToParentWindow("http://ebiz.cpic.com.cn/ywx/sysLogin.do?method=isLogin");
//                tbPage.waitAjaxInfo();
                action.pause(2500);
                homePage.switchRight();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Test(dataProvider = "data")
    public void toubao1(String tbr,String tbrcardNo,String insuredName,String insuredCardNo,String count,String email,String schooName) throws Exception{
        if(tbr.contains("杨琳")){
            String newTbr = StringUtils.replaceChars(tbr," "," ");
            System.out.println(newTbr.trim()+"88888888888888"+tbr);
        }
    }
}
